﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VentaElectrodomesticos.Sql;
using System.Data.SqlClient;

namespace VentaElectrodomesticos.AbmRol
{
    public partial class FormBajaRol : Form
    {
        private ClaseSQL sql;

        public FormBajaRol()
        {
            InitializeComponent();
            sql = new ClaseSQL();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                this.dataGridView1.Rows.Clear();
                sql.Open();

                String defaultquery = "SELECT idRol, nomRol FROM LAG.roles WHERE nomRol like '%" + this.textBoxRol.Text + "%' AND status = 1"; 
                SqlDataReader reader = sql.busquedaSQLDataReader(defaultquery);
                while (reader.Read())
                {
                    dataGridView1.Rows.Add(reader[0].ToString(), //id
                                          reader[1].ToString()); //nomRol 
                }
                reader.Close();
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message, "Error!");
            }
            finally
            {
                sql.Close();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.textBoxRol.Clear();
            this.dataGridView1.Rows.Clear();
        }


        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                sql.Open();
                String[,] parametros = new String[2, 1];
                String sp = "LAG.sp_bajaRol";
                parametros[0, 0] = "@ID";
                parametros[1, 0] = (String)dataGridView1[0, e.RowIndex].Value;
                SqlDataReader reader = sql.ejecutarStoredProcedure(sp, parametros);
                if (reader != null)
                {
                    MessageBox.Show("Se ha dado de baja el Rol " + (String)dataGridView1[1, e.RowIndex].Value , "Success");
                }

            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\n" + ex.StackTrace, "Error!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n" + ex.StackTrace, "Error app");
            }
            finally
            {
                sql.Close();
            }
        }

        private void buttonSalir_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
